// pages/jingxuan/index.js
var app = getApp()
var CONFIG = require("../../utils/config.js")
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: [],
    has_more: true,
    count: 0,
    loadingTip: '没有更多数据',
    myinfo:[],
    booklist:[],
    onstatus:1,
    viewId:1,
    // process:[],
    zhanglist:[],
    toInfo: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  myinfo:function(e){
    var that=this
    var count = that.data.count
    var id = e.currentTarget.dataset.id
    var openId=wx.getStorageSync('openId')
  
   
    if(id==0){
   
      that.setData({
        onstatus:0,
        count:0,
        booklist:[],
        viewId:0,
   
      })
      that.queryStore(openId)
    }else{
     
      that.setData({
        onstatus: 1,
        count: 0,
        booklist:[],
        viewId: 1,
        toInfo: true
      })
      that.queryHistory(openId)
    }
  },
  queryStore: function (openId) {
    var that = this
    var count = that.data.count
    var booklist = that.data.booklist
    var zhanglist = that.data.zhanglist
    wx.request({
      url: CONFIG.URL.queryStore,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        "wxuid": openId,
        "count": count
      },
      method: 'POSt',
      success: function (res) {

        if (res.data.booklist.length > 0) {
          console.log("===")
          var books = res.data.booklist

          var prolist = res.data.processlist
          var zhang = res.data.zhanglist
         
          var prolength = res.data.processlist.length
          var booklength = books.length
          for (var i = 0; i < booklength; i++) {
            for (var j = 0; j < prolength; j++) {
              if (books[i].id == prolist[j].bookid) {
            
                var num = books[i].num
                books[i].process = {};
                books[i].listnum={};
                books[i].zhang = {
                };
                books[i].listnum=i
                books[i].isStore=true
               
                books[i].zhang = zhang[j].zhang
                books[i].process = prolist[j].process
             
              }else{
        
                console.log(books[i])
              }
            }
          }
        
          that.setData({
            booklist: booklist.concat(res.data.booklist),
            count:res.data.booklist.length+count,
           
            
          })
          
        }
      }
    })
  },
  queryHistory: function (openId) {
    var that = this
    var count=that.data.count
    var booklist = that.data.booklist
    // var process = that.data.process
    wx.request({
      url: CONFIG.URL.queryHistory,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        "wxuid": openId,
        "count": count
      },
      method: 'POSt',
      success: function (res) {
  
        if(res.data.booklist.length>0){
          console.log("==3=")
          var books=res.data.booklist
         
          var prolist=res.data.processlist
          var zhang=res.data.zhanglist
      
          var prolength=res.data.processlist.length
          var booklength=books.length
          for(var i=0;i<booklength;i++){ 
          for(var j=0;j<prolength;j++){
          if(books[i].id==prolist[j].bookid){
            console.log("===")
          var num=books[i].num
           books[i].process={};
            books[i].zhang={
            };
         
            books[i].zhang=zhang[j].zhang
         
            if (prolist[j].process < 100 && prolist[j].process>0){
            books[i].process = prolist[j].process
          
            console.log(books[i])
            } else if (prolist[j].process >100){
              books[i].process=99
            }else{
              books[i].process = 1
            }
            console.log(books[i].process)
          }

          }
          }

          that.setData({
            booklist: booklist.concat(res.data.booklist),
            count: res.data.booklist.length + count,
          
          })
        }else{
          that.setData({
            has_more: false,
            loadingTip:"上拉加载更多"
          });
        }
      }
    })
  },
  cancelstore:function(e){
    console.log(e)
    var that=this
    var count=that.data.count
    var booklist = that.data.booklist

    console.log(booklist)

    var bookid = e.currentTarget.dataset.id
    var booklnegth = booklist.length
 
    var wxuid=wx.getStorageSync('openId')
    that.setData({
      toInfo:false,
    })
  

    var url = CONFIG.URL.cancelstore
    var data = {
      "wxuid": wxuid,
      "bookid": bookid
    };
    CONFIG.post_from(url, data, function (res) {
      console.log("===1=9=")
      console.log(res)
      if(res.data.code==200){
      for (var i = 0; i < booklnegth; i++) {
      if (booklist[i].id == bookid) {
        console.log(booklist[i])

        booklist[i].name = ''

      }
    }
      that.setData({
        count: count - 1,
        booklist:booklist,
        toInfo: true
      })
      wx.showToast({
        title: '删除成功',
      })
  
    that.queryStore(wxuid);
      }
    }, function (err) {
      console.log(err)
    })
  },
  queryUserBook: function (bookid){
    var that=this
    var wxuid=wx.getStorageSync('openId')
    var url=CONFIG.URL.queryUserBook
    var data={
      "wxuid":wxuid,
      "bookid":bookid
    };
    CONFIG.post_from(url,data,function(res){
      console.log("===1=8=")
      console.log(res)
    },function(err){
      console.log(err)
    })


  },
  queryBookInfo: function (bookid) {
    var that = this
    var wxuid = wx.getStorageSync('openId')
    var url = CONFIG.URL.queryBookInfo
    var data = {
      "wxuid": wxuid,
      "bookid": bookid
    };
    CONFIG.post_from(url, data, function (res) {
      console.log("====8=")
      console.log(res)
    }, function (err) {
      console.log(err)
    })


  },
  
  showDetail: function (e) {
    var that = this
    if(that.data.toInfo==false){
      return;
    }
    console.log(e)
    var bookid = e.currentTarget.dataset.id
    var booktitle = e.currentTarget.dataset.title
    app.globalData.booktitle = booktitle;
    var viewinfo =e.currentTarget.dataset.viewinfo;
    var process = e.currentTarget.dataset.process;
    console.log(process)
    app.showDetail(bookid,viewinfo,process)
  },
  onReachBottom: function () {
    var that = this
    var viewId=that.data.viewId
    console.log(viewId)
    if (that.data.has_more==false) {
      that.setData({
        loadingTip: "没有了哦~"
      });
      return;
    } else {

      var count = that.data.count
      that.setData({
        count: count
      });

      var  uid=wx.getStorageSync('openId')
      if(viewId==1){
      that.queryHistory(uid);
      }else{
        that.queryStore(uid)
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var openId = wx.getStorageSync('openId')
    var viewId = that.data.viewId
    if (!openId) {
      app.login()
    } else {
      that.setData({
        onstatus: 1,
        count: 0,
        booklist: [],
        viewId: 1,
        toInfo: true
      })
      that.queryHistory(openId)
     
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that=this
    that.setData({
      viewId:1,
      onstatus:1
    })
 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */


  /**
   * 页面上拉触底事件的处理函数
   */
 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
  
    return {
      title: '我在超好看的神经小说小程序上看书,你也来吧',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        console.log(res)
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})